import {createStorage} from "prorage";
import Cookies from 'js-cookie';

const lStorage = createStorage({
  storage: localStorage,
  prefix: 'YI_ZHUYI_'
})

const sStorage = createStorage({
  storage: sessionStorage,
  prefix: 'YI_ZHUYI_'
})



export default class StorageUtils {

  static USER_LOGIN_ACCOUNT_KEY = "YY_USER_ACCOUNT_LOGIN_"

  static DEVICE_IP_STORAGE_KEY = "_DEVICE_IP_STORAGE_"

  static LOGIN_TOKEN_STORAGE_KEY = "_SESSION_TOKEN_"

  static getCookieToken(account) {
    const tokenKey = 'YI_ZHUYI:' + account
    return Cookies.get(tokenKey)
  }

  static setCookieToken(account, token, rememberMe, expires) {
    const tokenKey = 'YI_ZHUYI:' + account
    console.log('cooke:::', Cookies)
    if (rememberMe) {
      return Cookies.set(tokenKey, token, { expires: expires })
    } else return Cookies.set(tokenKey, token)
  }

  static removeUserLoginToken(account) {
    this.delSessionStorage(StorageUtils.LOGIN_TOKEN_STORAGE_KEY)
  }

  static getUserLoginAccount() {
    return this.getSessionStorage(StorageUtils.USER_LOGIN_ACCOUNT_KEY)
  }

  static setUserLoginAccountToSession(account) {
    this.setSessionStorage(StorageUtils.USER_LOGIN_ACCOUNT_KEY, account)
  }

  static setSessionStorage(key, data) {
    sStorage[key] = data
  }

  static getSessionStorage(key) {
    return sStorage[key]
  }

  static delSessionStorage(key) {
    delete sStorage[key]
  }


  static setLocalStorage(key, data) {
    lStorage[key] = data
  }

  static getLocalStorage(key) {
    return lStorage[key]
  }

  static delLocalStorage(key) {
    delete lStorage[key]
  }

  static setDeviceIpStorage(data) {
    this.setLocalStorage(StorageUtils.DEVICE_IP_STORAGE_KEY, data)
  }

  static getDeviceIpStorage() {
    return this.getLocalStorage(StorageUtils.DEVICE_IP_STORAGE_KEY)
  }

  static removeLoginToken() {
    this.delSessionStorage(StorageUtils.USER_LOGIN_ACCOUNT_KEY)
  }

  static setUserLoginTokenStorage(token) {
    this.setSessionStorage(StorageUtils.LOGIN_TOKEN_STORAGE_KEY, token)
  }

  static getUserLoginToken() {
    return this.getSessionStorage(StorageUtils.LOGIN_TOKEN_STORAGE_KEY)
  }
}


